﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _4rumDCT.App_Code;
using System.Data;
using QLTB.Data;
using QLTB.Entities;

namespace QuanLyThietBi.DCTEDU_.QuanTri
{
    public partial class QLNguoiDung : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtTuKhoa.Attributes.Add("onkeypress", "return clickButton(event,'" + cmdTuKhoa.ClientID + "')");
                Pager1.CurrentIndex = 1;
                Bind_Data(Pager1.CurrentIndex);
            }
        }

        protected void cmdTuKhoa_Click(object sender, EventArgs e)
        {
            try
            {
                Pager1.CurrentIndex = 1;
                Bind_Data(Pager1.CurrentIndex);
            }
            catch (Exception ex)
            {
                Common.WriteLog("QLNguoiDung.aspx.cs", "cmdTimKiem_Click", ex.ToString());
            }
        }
        protected void Bind_Data(int PageNo)
        {
            int TotalRows = 0;
            string WhereClause = "1=1";
            WhereClause += " and (TenTruyCap like N'%" + txtTuKhoa.Text + "%' or HoTen like N'%" + txtTuKhoa.Text + "%')";
            DataSet dsHocVien = DataRepository.Provider.ExecuteDataSet("NguoiDung_GetPaged", WhereClause, "Id DESC", PageNo - 1, Pager1.PageSize);
            gridCatalog.DataSource = dsHocVien.Tables[0];
            gridCatalog.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsHocVien.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager1.ItemCount = TotalRows;
            if (TotalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }

        protected void Pager1_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
                Pager1.CurrentIndex = currnetPageIndex;
                Bind_Data(Pager1.CurrentIndex);
            }
            catch (Exception ex)
            {
                Common.WriteLog("QLNguoiDung.aspx.cs", "Pager1_Command()", ex.ToString());
            }
        }

        protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton l = (ImageButton)e.Row.FindControl("cmdDelete");
                l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");

                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
                e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
            }
        }

        protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                try
                {
                    H_CatalogId.Value = e.CommandArgument.ToString();
                    NguoiDung entNguoiDung = DataRepository.NguoiDungProvider.GetById(Convert.ToInt32(H_CatalogId.Value));
                    if (entNguoiDung != null)
                    {
                        txtTenDangNhap.Text = entNguoiDung.TenTruyCap;
                        txtHoTen.Text = entNguoiDung.HoTen;
                        Bind_Data(Pager1.CurrentIndex);
                    }
                }
                catch (Exception ex)
                {
                    Common.WriteLog("QLNguoiDung.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
                }
            }
            if (e.CommandName == "Delete")
            {
                try
                {
                    int IdNguoiDung = Convert.ToInt32(e.CommandArgument.ToString());
                    bool deleteStatus = DataRepository.NguoiDungProvider.Delete(IdNguoiDung);
                    if (!deleteStatus)
                        ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.XOA_THATBAI, true);
                    else
                        ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THANHCONG, Constant.XOA_THANHCONG, true);
                    Bind_Data(Pager1.CurrentIndex);
                }
                catch (Exception ex)
                {
                    Common.WriteLog("QLNguoiDung.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                    ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.XOA_THATBAI, true);
                }
            }
        }

        protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Bind_Data(Pager1.CurrentIndex);
            txtTenDangNhap.Text = "";
            txtMatKhau.Text = "";
            txtHoTen.Text = "";
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            bool updateStatus = false;
            try
            {
                string TenTruyCap = Common.StandardString(txtTenDangNhap.Text);
                if (H_CatalogId.Value.Length == 0 && DataRepository.NguoiDungProvider.GetByTenTruyCap(TenTruyCap) != null)
                {
                    ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.TENTRUYCAPDATONTAI, true);
                    return;
                }
                NguoiDung entNguoiDung = new NguoiDung();
                entNguoiDung.Id = Convert.ToInt32(H_CatalogId.Value);
                string MatKhau = Common.StandardString(txtMatKhau.Text);
                string MaTruyCap = TenTruyCap + MatKhau;
                entNguoiDung.TenTruyCap = TenTruyCap;
                entNguoiDung.MaTruyCap = Common.EncryptMD5(MaTruyCap);
                entNguoiDung.MatKhau = Common.EncryptMD5(MatKhau);
                entNguoiDung.HoTen = txtHoTen.Text;
                entNguoiDung.NgayCapNhat = Common.GetServerDateTime();
                switch (entNguoiDung.Id)
                {
                    case 0:
                        entNguoiDung.Id = 0;
                        updateStatus = DataRepository.NguoiDungProvider.Insert(entNguoiDung);
                        break;
                    default:
                        updateStatus = DataRepository.NguoiDungProvider.Update(entNguoiDung);
                        break;
                }

                if (updateStatus)
                {
                    ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THANHCONG, Constant.CAPNHAT_THANHCONG, true);
                    Bind_Data(Pager1.CurrentIndex);
                    reSet();
                }
                else
                    ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.CAPNHAT_THATBAI, true);
            }
            catch (Exception ex)
            {
                Common.WriteLog("QLNguoiDung.aspx.cs", "cmdSave_Click", ex.ToString());
                ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.CAPNHAT_THATBAI, true);
            }
        }

        public void reSet()
        {
            txtTenDangNhap.Text = "";
            txtMatKhau.Text = "";
            txtHoTen.Text = "";
        }
    }
}